package jwt;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;

public class GenerateRSAPublicKey {
    public static void main(String[] args) throws Exception {
        // 初始化 KeyPairGenerator
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048); // 指定密钥长度

        // 生成密钥对
        KeyPair keyPair = keyGen.generateKeyPair();

        // 提取公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

        PrivateKey aPrivate = keyPair.getPrivate();

        // 打印公钥
        System.out.println("Private Key (Base64): \n" +
                java.util.Base64.getEncoder().encodeToString(aPrivate.getEncoded()));

        // 打印公钥
        System.out.println("Public Key (Base64): \n" +
            java.util.Base64.getEncoder().encodeToString(publicKey.getEncoded()));
    }
}
